package engine.display.Story.ActionItem 
{
	import com.greensock.TweenLite;
	
	import engine.display.Story.ActionControl.TalkActionPlay;
	import engine.display.Story.ActionControl.TimerTextField;
	import engine.display.Story.StoryMoveEvent.StoryEvent;
	import engine.display.Story.StoryVo;
	import engine.flag.ResourcesPath;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import newx.com.ResManager;
	import newx.com.control.NxClip;
	import newx.com.control.text.NxLabel;
	
	/**
	 * ...
	 * 对话内容显示功能，
	 * @author WangHaiBo  www.woniu.com
	 */
	public class TalkInformationUI extends BaseAction
	{
		public var talkBG		:MovieClip;
		public var image		:NxClip;
		public var infoTxt		:TimerTextField;
		public var mousepic		:MovieClip;
		public var mouseTxt		:NxLabel;

		private var mouseImage:MovieClip;
		//对话内容数组
		private var _talkInfoArray	:Array;
		//文本对齐方式
		private var textAlign		:int	=	1;
		//图片的的朝向 1：右 2：左
		private var imageAlign		:int	=	1;
		//图片ID
		private var imageID			:String	=	"";
		//对话内容
		private var talkInfo		:String	=	"";
		//对话的人物名
		private var nameLabel		:TextField;
		//场景宽度
		private var _stageWidth		:int;
		private var _stageHeight	:int;
		private static var _self	:TalkInformationUI
		private var textFormat		:TextFormat;
		private var _textShowOver	:Boolean	=	false;
		private var _frameSpace	:Sprite;
		private var _talkMc		:TalkActionPlay;
		private var _iconType		:String;
		
		public var first			:Boolean=true;
		private var startPoint		:int;
		private var moveLong		:int;

		public function TalkInformationUI() 
		{
			Init();
		}
		public static function getInstance():TalkInformationUI
		{
			if (_self == null) 
			{
				_self	=	new TalkInformationUI()
			}
			return _self;
		}
		private function Init():void
		{
			_frameSpace	=	new Sprite();
			nameLabel	=	new TextField();
			infoTxt	=		new TimerTextField();
			image	=	new NxClip();
			talkBG	=	ResManager.newResClass("web_007") as MovieClip;
			mouseImage	=	ResManager.newResClass("web_008") as MovieClip;
			talkBG.width				=	1280;
			talkBG.height				=	235;
			
			_frameSpace.graphics.beginFill(0xFF8800);
			_frameSpace.graphics.drawRect(0,0,1280,768);
			_frameSpace.graphics.endFill();
			_frameSpace.alpha	=	0;
			
			infoTxt.selectable			=	nameLabel.selectable	=	false
			textFormat					=	new TextFormat();
			textFormat.size				=	18;
			textFormat.bold				=	true
			textFormat.color			=	0xFFFFFF;
			infoTxt.wordWrap			=	true;
			
			infoTxt.y					=	580;
			infoTxt.width				=	500;
			infoTxt.height				=	80;
			infoTxt.defaultTextFormat	=	textFormat;
			infoTxt.setTextFormat(textFormat);
			
			nameLabel.width				=	200;
			textFormat.color			=	0xFFFF00
			nameLabel.defaultTextFormat	=	textFormat;
			nameLabel.setTextFormat(textFormat);
			
			talkBG.y			=	480;
			image.y				=	450;
			mouseImage.y		=	650;
			nameLabel.y			=	510;
			
			this.addChild(talkBG);
			this.addChild(nameLabel);
			this.addChild(image);
			this.addChild(mouseImage);
			this.addChild(infoTxt);
			this.addChild(_frameSpace);
			mouseImage.visible	=	false;
			
		}
		/**
		 * 快速显示文本
		 * @param e
		 * 
		 */		
		private function clickHandle(e:MouseEvent):void
		{
			if(_textShowOver)
			{
				this.removeEventListener(MouseEvent.CLICK,clickHandle);
				clearTalkIcon();
			}
			else
			{
				infoTxt.showAll();
			}
		}
		private function talkOverHandle(e:StoryEvent):void 
		{
			_textShowOver		=	true;
			mouseImage.visible	=	true;
		}
		/**
		 * 显示内容信息
		 * @param	configID	所要显示的图片
		 * @param	talkInfo	对话内容
		 * @param	align		图片对齐方式
		 */
		override public function showInfo(val:StoryVo):void 
		{
			infoTxt.removeEventListener(StoryEvent.TEXT_OVER, talkOverHandle);
			infoTxt.text		=	"";
			_textShowOver		=	false;
			mouseImage.visible	=	false;
			image.source		=	"";
			textAlign			=	val.TextAlign;
			imageAlign			=	val.ImageAlign;
			imageID				=	val.ImageID;
			talkInfo			=	val.TalkInfo;
			_talkMc				=	val.actionNPC as TalkActionPlay;
			_iconType			=	val.IconType;
			alignItem(textAlign,imageAlign);
			ResourcesPath.getHalfPic(int(imageID),image);
			nameLabel.text	=	val.Name;
			
			moveUI();
		}
		
		/**
		 * 场景尺寸改变
		 */
		public function changeLocation(stageWidth:int,stageHeight:int):void
		{
			_stageWidth		=	stageWidth;
			_stageHeight	=	stageHeight;
			alignItem(textAlign, imageAlign);
		}		
		/**
		 * 文本是否显示完了
		 * @return 
		 * 
		 */		
		public function get textShowOver():Boolean
		{
			return _textShowOver;
		}
		private function moveUI():void
		{
			if(first)
			{
				this.y	=	300;
				first	=	false;
				TweenLite.to(this,.7,{y:"-300", onComplete:thisMoveHandle});
			}
			else
			{
				this.y	=	0
				thisMoveHandle()
			}

		}
		private function thisMoveHandle():void
		{
			TweenLite.to( image,.4, { x:moveLong,onComplete:againShowInfo});
		}
		/**
		 * 显示内容信息 
		 */	
		private function againShowInfo():void
		{
			infoTxt.addEventListener(StoryEvent.TEXT_OVER, talkOverHandle);
			this.addEventListener(MouseEvent.CLICK,clickHandle);
			_talkMc.TalkIcon	=	_iconType;
			infoTxt.text		=	talkInfo;
			
		}
		/**
		 * 一次性显示全部的内容
		 */		
		private function showTextAll():void
		{
			infoTxt.showAll();
		}
		/**
		 * 排列显示对象
		 * @param	val
		 */
		private function alignItem(textInt:int,imageInt:int):void
		{
			talkBG.x		=	0;
			talkBG.y		=	_stageHeight - talkBG.height;
			talkBG.width	=  _stageWidth;
			
			nameLabel.x		=	(_stageWidth - 200 ) * .5;
			infoTxt.x		=	(_stageWidth - 500 ) * .5;
			infoTxt.y		=	talkBG.y + 100;
			nameLabel.y		=	talkBG.y + 20;
			switch (textInt) 
			{
				case 1:
					
					mouseImage.x	=	_stageWidth - 200;
					break;
				case 2:
					
					
					mouseImage.x	=	30;
					break;
				default:
			}
			switch (imageInt) 
			{
				case 1:
					image.scaleX	= 1;
				break;
				case 2:
					image.scaleX	= -1;
				break;
				default:
			}
			if(textInt == 1)
			{
				if(imageInt == 1)
				{
					image.x		=	-300
					moveLong	=	2;
				}
				else
				{
					image.x		=	0
					moveLong	=	302;
				}
			}
			else
			{
				if(imageInt == 1)
				{
					image.x		=	_stageWidth;
					moveLong	=	_stageWidth - 300;
				}
				else
				{
					image.x		=	_stageWidth + 300;
					moveLong	=	_stageWidth;
				}
			}
		}
		private function clearTalkIcon():void
		{
			if(_talkMc != null)
			{
				_talkMc.clearIconSpace();
			}
			actionOver();
		}
	}

}